package web.jsfBean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.event.ValueChangeEvent;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.richfaces.component.html.HtmlToolBar;

import model.bean.ModuleBean;
import model.bean.UserBean;
import web.constant.WebConstant;
import web.helper.WebHelper;

public class AuthenticationJsfBean extends BaseJsfBean {

	private final static Logger log = Logger.getLogger(AuthenticationJsfBean.class);
	private final static String PAGE_LOGIN = "login";
	private final static String PAGE_MAIN = "main";
	private final static String PAGE_CHANGE_PASSWORD = "changePassword";

	public String authenticate() {
		try {
			HttpSession session = getSession(true);

			UserBean userBean = new UserBean();
			userBean.setUserLogin("Prueba");
			userBean.setUserFirstName("FRANK GABRIEL");
			userBean.setUserLastName("RODRIGUEZ LEE");

			session.setAttribute("userBean", userBean);
			setToolBarMenu(WebHelper.getToolBarMenu(getFacesContext(), getModuleList()));
		}
		catch (Exception e) {
			log.error(e.getMessage(), e);
			addMessage(FacesMessage.SEVERITY_ERROR, getResourceValue("systemError", null));
			return PAGE_LOGIN;
		}
		return PAGE_MAIN;
	}

	public void changeLanguage(ValueChangeEvent event) {
		try {
			HttpSession session = getSession(false);

			String language = event.getNewValue().toString();

			if (language.equals(WebConstant.SELECT_DEFAULT_VALUE)) {
				session.setAttribute("language", getApplication().getDefaultLocale().getLanguage());
			}
			else {
				session.setAttribute("language", language);
			}
		}
		catch (Exception e) {
			log.error(e.getMessage(), e);
			addMessage(FacesMessage.SEVERITY_ERROR, getResourceValue("systemError", null));
		}
	}

	public String changePassword() {
		try {
		}
		catch (Exception e) {
			log.error(e.getMessage(), e);
			addMessage(FacesMessage.SEVERITY_ERROR, getResourceValue("systemError", null));
			return PAGE_CHANGE_PASSWORD;
		}
		return PAGE_MAIN;
	}

	public String logout() {
		try {
			HttpSession session = getSession(false);
			session.invalidate();
		}
		catch (Exception e) {
			log.error(e.getMessage(), e);
			addMessage(FacesMessage.SEVERITY_ERROR, getResourceValue("systemError", null));
		}
		return PAGE_LOGIN;
	}

	public String cancel() {
		try {
		}
		catch (Exception e) {
			log.error(e.getMessage(), e);
			addMessage(FacesMessage.SEVERITY_ERROR, getResourceValue("systemError", null));
		}
		return PAGE_MAIN;
	}

	private List<ModuleBean> getModuleList() {
		List<ModuleBean> moduleList = new ArrayList<ModuleBean>();

		ModuleBean moduleBean = new ModuleBean();
		moduleBean.setModuleName(getResourceValue("maintenance", null));

		List<ModuleBean> subModuleList = new ArrayList<ModuleBean>();

		ModuleBean subModuleBean = new ModuleBean();
		subModuleBean.setModuleName(getResourceValue("parameterMaintenance", null));
		subModuleBean.setModuleUrl("#{personaJsfBean.inicializar}");
		subModuleList.add(subModuleBean);

		moduleBean.setModuleList(subModuleList);
		moduleList.add(moduleBean);

		return moduleList;
	}

	private String user;
	private String password;
	private String newPassword;
	private String confirmNewPassword;
	private String language;
	public HtmlToolBar toolBarMenu;

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getConfirmNewPassword() {
		return confirmNewPassword;
	}

	public void setConfirmNewPassword(String confirmNewPassword) {
		this.confirmNewPassword = confirmNewPassword;
	}

	public HtmlToolBar getToolBarMenu() {
		return toolBarMenu;
	}

	public void setToolBarMenu(HtmlToolBar toolBarMenu) {
		this.toolBarMenu = toolBarMenu;
	}
}